#include <stdio.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> 
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <signal.h>
#include <pthread.h>
#include "cJSON.h"

// 天气数据结构体
struct weather_data
{
    char date[50];
    char temperature[50];
    char weather[50];
    char manner[50];
    char pm[50];
};

// 定义未来6天的天气
struct weather_data day[6];

// 获取天气函数
void get_weather(char * city, char * city_addr, char * date_h, char * date_day)  // char *city, char * date_h, char * date_day
{
    // 0、清空天气数组
    memset(day, 0, sizeof(day));

    // 1、创建一个TCP的客户端对象
    int tcp_socket = socket(AF_INET, SOCK_STREAM, 0);

    // 2、连接http服务器 183.136.132.78(apii.cn的服务器地址)
    struct sockaddr_in server_addr;
    server_addr.sin_family      = AF_INET;   
    server_addr.sin_port        = htons(80);
    server_addr.sin_addr.s_addr = inet_addr("183.136.132.78");

    int ret = connect(tcp_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if (ret < 0)
    {
        perror("connect error!\n");
        return ;
    }

    // 3、发送http请求
    // http://apii.cn/api/weather/?city=北京
    char http_request[2048] = {0};
    char city_buf[1024]     = {0};
    printf("请输入要查询天气的城市名称:\n");
    scanf("%s", city_buf);

    // 拼接请求和提问
    sprintf(http_request, "GET /api/weather/?city=%s HTTP/1.1\r\nHost:www.apii.cn\r\n\r\n", city_buf);
    write(tcp_socket, http_request, strlen(http_request));

    // 4、读取http服务器的应答数据
    char buf[4096] = {0};
    read(tcp_socket, buf, sizeof(buf));
    // printf("服务器应答数据:%s\n", buf);

    // 5、关闭socket
    close(tcp_socket);

    // ========================================== //

    // 6、http去头
    char *p = strstr(buf, "{");
    // printf("p = %s\n", p);

    // 7、http去尾
    char *q = p;
    while (*q != '\r')
    {
        q++;
    }
    *q = '\0';
    // printf("p = %s\n", p);

    // 8、把字符串转换为json数据
    char json_data[4096] = {0};
    strcpy(json_data, p);
    
    cJSON *json_obj = cJSON_Parse(json_data); 
    if (json_obj == NULL)
    {
       // 获取错误的原因
       const char *err = cJSON_GetErrorPtr();  
       printf("解析cjsn失败:%s\n", err);
    }
    else
    {
        printf("解析json成功\n");
    }

    // 获取城市
    cJSON * _city = cJSON_GetObjectItem(json_obj, "city");
    strcpy(city_addr, cJSON_GetStringValue(_city));

    cJSON * _date_h = cJSON_GetObjectItem(json_obj, "date_h");
    strcpy(date_h, cJSON_GetStringValue(_date_h));

    cJSON * _date_day = cJSON_GetObjectItem(json_obj, "date_day");
    strcpy(date_day, cJSON_GetStringValue(_date_day));

    // 获取未来6天的天气
    cJSON * data_arry = cJSON_GetObjectItem(json_obj, "data");

    for(int i = 0; i < cJSON_GetArraySize(data_arry); i++) 
	{
        // 获取数组项
        cJSON * data = cJSON_GetArrayItem(data_arry, i);

        strcpy(day[i].date, cJSON_GetObjectItem(data, "date")->valuestring);
        strcpy(day[i].temperature, cJSON_GetObjectItem(data, "temperature")->valuestring);
        strcpy(day[i].weather, cJSON_GetObjectItem(data, "weather")->valuestring);
        strcpy(day[i].manner, cJSON_GetObjectItem(data, "manner")->valuestring);
        strcpy(day[i].pm, cJSON_GetObjectItem(data, "pm")->valuestring);
    }

    // 关闭socket
    close(tcp_socket);


}

// 主函数
int main(int argc, char const *argv[])
{

    // 获取天气
    char city_addr[100] = {0};
    char date_h[100]    = {0};
    char date_day[100]  = {0};
    get_weather("北京", city_addr, date_h, date_day);

    // 打印测试
    printf("city_addr:%s\n", city_addr);
    printf("date_h:%s\n", date_h);
    printf("date_day:%s\n", date_day);
	
	for(int i = 0; i < 6; i++) 
	{
        printf("day[%d].date:%s\n", i, day[i].date);
        printf("day[%d].temperature:%s\n", i, day[i].temperature);
        printf("day[%d].weather:%s\n", i, day[i].weather);
        printf("day[%d].manner:%s\n", i, day[i].manner);
        printf("day[%d].pm:%s\n", i, day[i].pm);
        printf("\n");
    }
}